package com.example.demo.model;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
@AllArgsConstructor
public class Response<T> {
    @ApiModelProperty("0成功，负数出错")
    private Integer code;
    private String msg;
    private T data;

    public static <T> Response<T> success(T data) {
        return new Response<>(0, null, data);
    }

    public static <T> Response<T> fail(Integer code) {
        String msg;
        if (code == -1) {
            msg = "未登录或登录凭证过期";
        } else if (code == -2) {
            msg = "用户名不存在"; // 直接注册
        } else if (code == -3) {
            msg = "密码错误";
        } else if (code == -4) {
            msg = "已登录";
        } else if (code == -5) {
            msg = "权限不足";
        } else if (code == -6) {
            msg = "队列空";
        } else {
            msg = "未知的错误代码";
        }
        return new Response<>(code, msg, null);
    }

    public static <T> Response<T> fail(Integer code, String msg) {
        return new Response<>(code, msg, null);
    }

    public static <T> Response<T> fail(Integer code, T data) {
        Response response = fail(code);
        response.setData(data);
        if (data != null) {
            log.error("错误代码:{},错误信息:{},错误数据:{}", code, response.msg, data);
        }
        return response;
    }
}
